/*
 * Copyright (c) 2018-2999 九五云信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.platform.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.AttachFile;
import com.yami.shop.common.bean.Qiniu;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.config.ShopConfig;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.AttachFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.Objects;

/**
 * 文件上传 controller
 * @author lgh
 *
 */
@RestController
@RequestMapping("/admin/file")
@Api(tags = "平台端文件上传接口")
public class FileController {

	@Autowired
	private AttachFileService attachFileService;
	@Autowired
	private ShopConfig shopConfig;

	@PostMapping("/upload/element")
	@ApiOperation(value = "上传Element UI文件", notes = "上传Element UI组件使用的文件")
	public ResponseEntity<String> uploadElementFile(@ApiParam(value = "上传的文件", required = true) @RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
            return ResponseEntity.noContent().build();
        }
		String fileName = attachFileService.uploadFile(file.getBytes(),file.getOriginalFilename());
        return ResponseEntity.ok(fileName);
	}

	/**
	 * 上传聊天时的文件
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/upload/imFile")
	@ApiOperation(value = "上传聊天文件", notes = "上传即时通讯聊天时使用的文件")
	public ResponseEntity<String> uploadImFile(@ApiParam(value = "上传的聊天文件", required = true) @RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		String fileName = attachFileService.uploadImFile(file.getBytes(),file.getOriginalFilename());
		return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/img")
	@ApiOperation(value = "上传图片", notes = "上传平台图片，并保存图片信息到数据库")
	public ResponseEntity<String> uploadImg(@ApiParam(value = "上传的图片文件", required = true) @RequestParam("file") MultipartFile file) throws IOException{
		if(file.isEmpty()){
			return ResponseEntity.noContent().build();
		}
		AttachFile attachFile = new AttachFile();
		attachFile.setFileType(FileUtil.extName(file.getOriginalFilename()));
		attachFile.setFileName(FileUtil.mainName(file.getOriginalFilename()));
		attachFile.setType(1);
		attachFile.setShopId(Constant.PLATFORM_SHOP_ID);
		attachFile.setUploadTime(new Date());
		String fileName = attachFileService.uploadImg(file.getBytes(),attachFile);
		return ResponseEntity.ok(fileName);
	}

	@PostMapping("/upload/tinymceEditor")
	@ApiOperation(value = "上传富文本编辑器图片", notes = "上传TinyMCE富文本编辑器使用的图片")
	public ResponseEntity<String> uploadTinymceEditorImages(@ApiParam(value = "上传的编辑器图片", required = true) @RequestParam("editorFile") MultipartFile editorFile) throws IOException{
		String fileName =  attachFileService.uploadFile(editorFile.getBytes(),editorFile.getOriginalFilename());
        return ResponseEntity.ok(shopConfig.getDomain().getResourcesDomainName() + "/" + fileName);
	}

	/**
	 * 分页获取历史图片
	 */
	@GetMapping("/attachFilePage")
	@ApiOperation(value = "分页获取历史图片", notes = "分页获取平台的历史上传图片")
	public ResponseEntity<IPage<AttachFile>> attachFilePage(PageParam<AttachFile> page, AttachFile attachFile) {
		attachFile.setShopId(Constant.PLATFORM_SHOP_ID);
		IPage<AttachFile> attachFilePage = attachFileService.getPage(page,attachFile);
//		IPage<AttachFile> attachFilePage = attachFileService.page(page,new LambdaQueryWrapper<AttachFile>()
//				.eq(AttachFile::getShopId,Constant.PLATFORM_SHOP_ID)
//				.eq(AttachFile::getType,1)
//				.like(StrUtil.isNotBlank(attachFile.getFileName()),AttachFile::getFileName,attachFile.getFileName())
//				.orderByDesc(AttachFile::getUploadTime));
		return ResponseEntity.ok(attachFilePage);
	}
	/**
	 * 删除图片
	 */
	@DeleteMapping("/deleteFile/{fileId}")
	@ApiOperation(value = "删除图片", notes = "根据文件ID删除已上传的图片")
	public ResponseEntity<Void> deleteFile(@ApiParam(value = "文件ID", required = true) @PathVariable("fileId") Long fileId){
		AttachFile attachFile = attachFileService.getById(fileId);
		attachFileService.deleteFile(attachFile.getFilePath());
		return ResponseEntity.ok().build();
	}


	/**
	 * 更改图片名称
	 */
	@PutMapping("/updateFileName")
	@ApiOperation(value = "更新图片名称", notes = "更新已上传图片的文件名")
	public ResponseEntity<Boolean> updateFileName(@ApiParam(value = "图片信息", required = true) @RequestBody  AttachFile attachFile) {
		if (Objects.isNull(attachFile.getFileName())){
			// 图片名称不能为空
			throw new YamiShopBindException("yami.img.name.exist");
		}
		return ResponseEntity.ok(attachFileService.updateFileName(attachFile));
	}
}
